package com.yc.jpyy.base.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.yc.jpyy.base.ketama.HashAlgorithm;
import com.yc.jpyy.base.ketama.KetamaNodeLocator;
import com.yc.jpyy.base.ketama.Node;
import com.yc.jpyy.base.redis.IRedisManager;



public class SysConfig {
	
	Map<String, String> cacheConfigMap;
	List<Node> cacheNodes;
	
	public Map<String, String> getCacheConfigMap() {
		return cacheConfigMap;
	}

	public void setCacheConfigMap(Map<String, String> cacheConfigMap) {
		this.cacheConfigMap = cacheConfigMap;
	}
	
	/**
	 * 缓存服务
	 * @param code
	 * @return
	 */
	public static Node getCacheNode(Object code) {
		SysConfig sysConfig = (SysConfig) SpringWebContextUtil.getApplicationContext().getBean("sysConfig");
		if (null == sysConfig.cacheNodes) {
			sysConfig.cacheNodes = new ArrayList<Node>();
			Iterator<Map.Entry<String, String>> iterator = sysConfig.cacheConfigMap.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<String, String> entry = iterator.next();
				Node node = new Node(entry.getKey(), entry.getValue().toString());
				IRedisManager redisManager=(IRedisManager)SpringWebContextUtil.getApplicationContext().getBean(node.getKey());
				/*try {
					//redisManager.getKeyValue("hello")
					System.out.println("redisManager=== " );
				} catch (BissinessException e) {
					e.printStackTrace();
				}*/
				sysConfig.cacheNodes.add(node);
			}
		}
		KetamaNodeLocator locator = new KetamaNodeLocator(sysConfig.cacheNodes, HashAlgorithm.KETAMA_HASH, 500);
		return locator.getPrimary(Integer.toString(code.hashCode()));
	}
	
}
